home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Borland Plateform / TURBO PASCAL 1.5 for WIN / PAINT.PAK / PAINTDEF.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-06-08  |  8.2 KB  |  300 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Pascal for Windows: Paint Demo         }
  4. {   paintdef unit                                }
  5. {   Copyright (c) 1992 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. unit PaintDef;
  10.  
  11. { This unit supplies the basic definitions used by all modules of the
  12.   paint program, as well as a few utility routines dealing mainly
  13.   with common dialogs.
  14. }
  15.  
  16. interface
  17.  
  18. uses ResDef, StdDlgs, WinTypes, WinProcs, WObjects;
  19.  
  20. var 
  21.   DashedPen: HPen;
  22.  
  23. type
  24.  
  25.   PPaintTool = ^TPaintTool;
  26.  
  27.   { The state object is used for communication among modules of the
  28.     paint program. It records the state of drawing, i.e., what has
  29.     been drawn, what colors, pen, brush, etc. are selected, what the
  30.     current screen selection is, etc. Only one state object should
  31.     exist for each paint screen.
  32.   }
  33.  
  34.   PState = ^TState;
  35.   TState = record
  36.     PaintTool: PPaintTool;      { Tool currently in use }
  37.     MemDC: HDC;            { Display contex with the offscreen Bitmap }
  38.     Offset: TPoint;        { Offset of Bitmap origin relative to screen }
  39.     BitmapSize: TPoint;            { Size of current bitmap }
  40.     IsDirtyBitmap: Boolean;      { Records when the Bitmap has been modified }
  41.     Selection: TRect;           { Coordinates of current screen selection }
  42.     SelectionBM: HBitmap;       { Contents of current screen selection }
  43.     PenSize: Integer;           { Current line width selected }
  44.     PenColor: TColorRef;        { Current line color selected }
  45.     BrushColor: TColorRef;      { Current fill color selected }
  46.   end;
  47.  
  48.  
  49.   { Paint tools are the basic entities that cause painting to be done
  50.     in the paint program. PaintTool defines the interface required by
  51.     all PaintTools, but no implementation (save initialization).
  52.   }
  53.  
  54.   TPaintTool = object(TObject)
  55.     Icon: HIcon;                { Icon associated with tool }
  56.     Cursor: HCursor;            { Cursor to be displayed when tool is
  57.                                   in use }
  58.     State: PState;              { Current state of drawing }
  59.     Window: HWnd;               { Window tool is operating on }
  60.     DC: HDC;                    { Screen display context to be operated on }
  61.  
  62.     { Creation and activation }
  63.     constructor Init(AState: PState; IconName, CursorName: PChar);
  64.     procedure Select; virtual;
  65.     procedure Deselect; virtual;
  66.  
  67.     { Actions initiated by mouse action }
  68.     procedure MouseDown(AWindow: HWnd; X, Y: Integer;
  69.       AState: PState); virtual;
  70.     procedure MouseMove(X, Y: Integer); virtual;
  71.     procedure MouseUp; virtual;
  72.     procedure DrawBegin(X, Y: Integer); virtual;
  73.     procedure DrawTo(X, Y: Integer); virtual;
  74.     procedure DrawEnd; virtual;
  75.  
  76.     { Utility routines used by mouse actions }
  77.     procedure PickUpSelection(aDC: HDC; Left, Top, aWidth, aHeight: Integer);
  78.       virtual;
  79.     procedure ReleaseSelection; virtual;
  80.     procedure DropSelection; virtual;
  81.  
  82.     { Actions initiated by keyboard }
  83.     procedure Char(Key, Count, lParamHi: Word); virtual;
  84.   end;
  85.  
  86. { Utility routines }
  87.  
  88. { Dialog Interactions }
  89.  
  90. { Display a message in a dialog with certain standard buttons.
  91. }
  92. function Ask(Quest: PChar): Boolean;            { Yes/No }
  93. function AskCancel(Quest: PChar): Integer;      { Yes/No/Cancel }
  94. function Confirm(Msg: PChar): Boolean;          { Ok/Cancel }
  95. procedure Tell(Msg: PChar);                     { Ok }
  96.  
  97.   Display standard file dialogs. Path may contain a mask (e.g.,
  98.   '*.pas') and contains full path name on return. Function return
  99.   value is True if file was selected, False on Cancel. 
  100. }
  101. function FileOpenDialog(Path: PChar): Boolean;  { File open }
  102. function FileSaveDialog(Path: PChar): Boolean;  { Filename selection }
  103.  
  104. { Other }
  105. function CreateCompatibleDCW(HWindow: Hwnd): HDC;
  106.  
  107. implementation
  108.  
  109. { TPaintTool }
  110.  
  111. { Default initialization of a Paint Tool.
  112. }
  113. constructor TPaintTool.Init(AState: PState; IconName, CursorName: PChar);
  114. begin
  115.   TObject.Init;
  116.   State := AState;
  117.   Icon := LoadIcon(HInstance, IconName);
  118.   Cursor := LoadCursor(HInstance, CursorName);
  119. end;
  120.  
  121. { Set up the paint tool to be the currently used tool.
  122. }
  123. procedure TPaintTool.Select;
  124. begin
  125.   State^.PaintTool := @Self;
  126. end;
  127.  
  128. {
  129.   Prepare the paint tool to no longer be the currently used tool.
  130. }
  131. procedure TPaintTool.Deselect;
  132. begin
  133. end;
  134.  
  135. { Actions initiated by mouse actions. }
  136.  
  137. { Action to be taken when the mouse button is pressed down (and the
  138.   tool is the currently used tool).
  139. }
  140. procedure TPaintTool.MouseDown(AWindow: HWnd; X, Y: Integer;
  141.   AState: PState);
  142. begin
  143. end;
  144.  
  145. { Action to be taken when the mouse button is down and the mouse is moved.
  146. }
  147. procedure TPaintTool.MouseMove(X, Y: Integer);
  148. begin
  149. end;
  150.  
  151. { Action to be taken when the mouse button is released.
  152. }
  153. procedure TPaintTool.MouseUp;
  154. begin
  155. end;
  156.  
  157. { Prepare the tool to begin drawing. Used by tools whose actions are
  158.   in response to mouse clicks. Their drawing actions are divided into
  159.   three phases: 1) DrawBegin on mouse down, 2) DrawTo when the mouse
  160.   is moved, and 3) DrawEnd when the mouse is released.
  161. }
  162. procedure TPaintTool.DrawBegin(X, Y: Integer);
  163. begin
  164. end;
  165.  
  166. { Perform the tool drawing action.
  167. }
  168. procedure TPaintTool.DrawTo(X, Y: Integer);
  169. begin
  170. end;
  171.  
  172. { Prepare the tool to stop drawing.
  173. }
  174. procedure TPaintTool.DrawEnd;
  175. begin
  176. end;
  177.  
  178.  
  179. { Utility routines used by mouse action routines.
  180.  
  181. { Prepare the rectangle selected on the screen to be treated as
  182.   a distinct entity. E.g., for dragging or cutting.
  183. }
  184. procedure TPaintTool.PickUpSelection(aDC: HDC; Left, Top, 
  185.                                                aWidth, aHeight: Integer);
  186. begin
  187. end;
  188.  
  189. { Release the current selection without modifying the current Bitmap.
  190. }
  191. procedure TPaintTool.ReleaseSelection;
  192. begin
  193. end;
  194.  
  195. { Copy the current selection onto the current Bitmap and release the
  196.   selection. 
  197. }
  198. procedure TPaintTool.DropSelection;
  199. begin
  200. end;
  201.  
  202. { Action initiated by the keyboard.
  203.  
  204. { Action to be taken when a non-system key is pressed. That is, not an
  205.   "alt" or other specially interpreted key-stroke.
  206. }
  207. procedure TPaintTool.Char(Key, Count, lParamHi: Word);
  208. begin
  209. end;
  210.  
  211. { Utility routines }
  212.  
  213. { Display a message in a dialog box with certain common buttons.
  214. }
  215. { Yes/No }
  216. function Ask(Quest: PChar): Boolean;
  217. begin
  218.   Ask := MessageBox(0, Quest, '', mb_YesNo) = id_Yes;
  219. end;
  220.  
  221. { Yes/No/Cancel }
  222. function AskCancel(Quest: PChar): Integer;
  223. begin
  224.   AskCancel := MessageBox(0, Quest, '', mb_YesNoCancel);
  225. end;
  226.  
  227. { Ok/Cancel }
  228. function Confirm(Msg: PChar): Boolean;
  229. begin
  230.   Confirm := MessageBox(0, Msg, '', mb_OkCancel) = id_OK;
  231. end;
  232.  
  233. { Ok }
  234. procedure Tell(Msg: PChar);
  235. begin
  236.   MessageBox(0, Msg, '', mb_Ok);
  237. end;
  238.  
  239.  
  240. { File Dialogs }
  241.  
  242. { Display a standard file dialog. Path will be filled in with the
  243.   selected filename (full path). Which is a "sd_" constant specifying
  244.   which file dialog to display.
  245. }
  246. function FileDialog(var Path: PChar; Which: PChar): Boolean;
  247. begin
  248.   FileDialog := 
  249.   Application^.ExecDialog(new(PFileDialog, Init(Application^.MainWindow,
  250.     Which, Path))) = id_OK;
  251. end;
  252.  
  253. { Standard file open. (Select an existing file.) }
  254. function FileOpenDialog(Path: PChar): Boolean;
  255. begin
  256.   FileOpenDialog := FileDialog(Path, PChar(sd_FileOpen));
  257. end;
  258.  
  259. { Standard file save. (Select a new or existing file.) }
  260. function FileSaveDialog(Path: PChar): Boolean;
  261. begin
  262.   FileSaveDialog := FileDialog(Path, PChar(sd_FileSave));
  263. end;
  264.  
  265. { Other }
  266.  
  267. { Given a window, return a drawing context that is compatible with
  268.   that window.
  269. }
  270. function CreateCompatibleDCW(HWindow: Hwnd): HDC;
  271. var
  272.   DC: HDC;
  273. begin
  274.   DC := GetDC(HWindow);
  275.   CreateCompatibleDCW :=  CreateCompatibleDC(DC);
  276.   ReleaseDC(HWindow, DC);
  277. end;
  278.  
  279. { Deal with deinitialization of unit. }
  280. var
  281.   SaveExit: Pointer;
  282.  
  283. procedure PaintDefExit;
  284. far;
  285. begin
  286.   DeleteObject(DashedPen);
  287.   ExitProc := SaveExit;
  288. end;
  289.  
  290. { initialization }
  291. begin
  292.   { A pen that draws a dashed line. }
  293.   DashedPen := CreatePen(ps_Dot, 1, $000000);
  294.  
  295.   { Set up unit de-initialization }
  296.   SaveExit := ExitProc;
  297.   ExitProc := @PaintDefExit;
  298. end.
  299.